import Vue from 'vue'
import Router from 'vue-router'
import {getToken} from "../../../util";
import config from '@/config'

Vue.use(Router)

const ssoUrl = process.env.NODE_ENV === 'development' ? config.ssoUrl.dev : config.ssoUrl.pro

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: () => import('@/components/page/Index.vue'),
    }
  ],
})

router.beforeEach((to, from, next) => {
  const token = getToken()
  console.log(token)
  if (token) {
    window.location.href = '/home.htm'
    return
  } else {
    let url = "https://fedlogin.jnj.com/as/authorization.oauth2?client_id=CHINA-CONSUMER-STORE-MASTER&client_secret=BwUVV05OSWp867TOLTa6I1enqNSe3152wrrlPek1FttNPGSmUhZyKl68vS66XQ0P&response_type=code&redirect_uri="+encodeURIComponent(ssoUrl)
      + "&nonce=" + RondomPass(10)
    console.log('111：',url)
    window.location.href = url
    return
  }
  next()
})

function RondomPass(number){
  let arr = new Array;
  let arr1 = new Array("0","1","2","3","4","5","6","7","8","9");
  let nonceStr=''
  for(var i=0;i<number;i++){
    let n = Math.floor(Math.random()*10);
    arr[i] =arr1[n] ;
    nonceStr+=arr1[n];
  }
  console.log('222：',nonceStr)
  return nonceStr
}

export default router
